package com.wqmei.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.json.Json;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wqmei.model.Person;
import com.wqmei.service.IPersonService;
import com.wqmei.utils.Constant;

@RestController
public class PersonController {

	@Autowired
	private IPersonService personService;
	
	/**
	 * 查询所有用户
	 * @return
	 */
	@RequestMapping(value="person",method=RequestMethod.GET)
	public Map<String, Object> getAllPerson(){
		Map<String, Object> result = new HashMap<String,Object>();
		List<Person> persons = personService.selectAllPeople();
		result.put("person", persons);
		result.put("code", Constant.OK);
		return result;
	}
	
	/**
	 * 根据主键查询用户
	 * @param id
	 * @return
	 */
	@RequestMapping(value="person/{id}",method=RequestMethod.GET)
	public Map<String, Object> getPersonById(@PathVariable int id){
		Map<String, Object> result = new HashMap<String,Object>();
		Person persons = personService.selectPerson(id);
		result.put("person", persons);
		result.put("code", Constant.OK);
		return result;
	}
	
	/**
	 * 根据号码模糊查找用户
	 * @param number
	 * @return
	 */
	@RequestMapping(value="getPersonByNumber",method=RequestMethod.GET)
	public Map<String, Object> getPersonByName(@RequestParam("number") String number){
		Map<String, Object> result = new HashMap<String,Object>();
		List<Person> persons = personService.selectAllPeople();
		List<Person> person = personService.selectPersonByNumber(number);
		result.put("person", person);
		return result;
	}
	
	/**
	 * 根据主键删除用户
	 * @param id
	 * @return
	 */
	@RequestMapping(value="deletePerson/{id}",method=RequestMethod.DELETE)
	public Map<String, Object> deletePerson(@PathVariable int id){
		Map<String, Object> result = new HashMap<String,Object>();
		int count = personService.deletePerson(id);
		if(count>0)
		result.put("code",Constant.OK);
		else
			result.put("code", Constant.FAIL);
		return result;
	}
	
	/**
	 * 添加用户
	 * @param person
	 * @return
	 */
	@RequestMapping(value="addPerson",method=RequestMethod.POST)
	public Map<String, Object> addPerson(@RequestBody Person person){
		Map<String, Object> result = new HashMap<String,Object>();
		int count = personService.insertPerson(person);
		if(count>0){
			System.out.println("添加成功,被影响的行数为"+count+"行");
		result.put("code",Constant.OK);}
		else
			result.put("code", Constant.FAIL);
		return result;
	}
	
	/**
	 * 修改用户
	 * @param person
	 * @return
	 */
	@RequestMapping(value="updatePerson",method=RequestMethod.PUT)
	public Map<String, Object> updatePerson(@RequestBody Person person){
		Map<String, Object> result = new HashMap<String,Object>();
		int count = personService.updatePerson(person);
		if(count>0){
			System.out.println("修改,被影响的行数为"+count+"行");
		result.put("code",Constant.OK);
		return result;}
		else
			result.put("code", Constant.FAIL);
		return result;
	}
	
	/**
	 * 测试接受参数
	 * @param id
	 * @return
	 */
	@RequestMapping(value="test",method=RequestMethod.POST)
	public Map<String, Object> Test(@RequestBody Person person){
		Map<String,Object> result = new HashMap<String,Object>();
		System.out.println(person.toString());
		result.put("code",Constant.OK);
		return result;
	}
}
